// import axios from 'axios'

// export default function (options) {
//   let DefaultParams = {
//     url: '',
//     method: 'get',
//     credentials: 'include'
//   }
//   options = Object.assign({}, DefaultParams, options)
//   try {
//     return axios(options).then((response) => {
//       let {headers, data} = response
//       let contentType = headers['content-type']
//       if (contentType && contentType.indexOf('application/json') !== -1) {
//         // let {status, message} = data
//         // 可对业务接口返回的约定状态值进行定制处理
//         // if (status !== 0) {
//         //   return Promise.reject(new Error(message))
//         // }
//         return Promise.resolve(data)
//       } else {
//         return Promise.reject(new Error('the response is not JSON'))
//       }
//     })
//   } catch (e) {
//     console.error('axios error: ', e)
//   }
// }

import { Toast } from 'antd-mobile';

import axios from "axios";

axios.defaults.withCredentials = true; //让ajax携带cookie

let HOST;

if (process.env.NODE_ENV == "development") {
  //开发环境
  HOST = "/api";
  console.log('测试',process.env.NODE_ENV)
} else {
  //生产环境 测试专用，上线将修改为正式地址
  HOST = "/api";
  //HOST = "https://test.51anxintou.com/api";
  console.log('生产',process.env)
}




export function fetch(url, method = "GET",params, header = {}) {
  Toast.loading('加载中', 1);
  // setTimeout(()=>{
  //   Toast.hide()
  // },3000)
 // Toast.success('Load success !!!', 1);
 
  return new Promise((resolve, reject) => {
    axios({
      method: method,
      url: HOST + url,
      data:params,
      timeout: 10000,
      headers: header,
      transformRequest: [function (data) {
        let ret = ''
        for (let it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }],
    })
      .then(response => {
        Toast.hide()
        if (response) {
          Toast.hide()
          resolve(response.data);
        }
      },error=>{
        Toast.hide()
        reject(error);
      })
      .catch(error => {
        Toast.hide()
        reject(error);
      });
  });
}


